package com.wyjy.bkd.utils;

import android.content.Context;
import android.content.SharedPreferences;


public class SpUtils {

    private final       String TOKEN_KEY   = "token";
    private final       String UID_KEY     = "userid";

    private final  SharedPreferences.Editor mEditor;
    private final  SharedPreferences        mSp;
    private static SpUtils                  spUtils;


    public static SpUtils getInstance(Context context) {
        if (spUtils == null) {
            spUtils = new SpUtils(context);
        }
        return spUtils;
    }

    public SpUtils(Context context) {
        mSp = context.getSharedPreferences("CAPPUSharedPreferences", 0);
        mEditor = mSp.edit();
    }

    /*--------------- 读 ---------------*/

    public String getString(String key, String defValue) {
        return mSp.getString(key, defValue);
    }

    /**
     * 读取整型
     */
    public int getInt(String key, int defValue) {
        return mSp.getInt(key, defValue);
    }

    /**
     * 读取long
     */
    public long getLong(String key, long defValue) {
        return mSp.getLong(key, defValue);
    }

    /**
     * 读取布尔型
     */
    public boolean getBoolean(String key, boolean defValue) {
        return mSp.getBoolean(key, defValue);
    }


    /*--------------- 写 ---------------*/
    public void putString(String key, String value) {
        mEditor.putString(key, value);
        mEditor.commit();
    }

    /**
     * 写入int
     *
     * @param key
     * @param value
     */
    public void putInt(String key, int value) {
        mEditor.putInt(key, value);
        mEditor.commit();
    }

    /**
     * 写入long
     *
     * @param key
     * @param value
     */
    public void putLong(String key, long value) {
        mEditor.putLong(key, value);
        mEditor.commit();
    }

    /**
     * 写入boolean
     *
     * @param key
     * @param value
     */
    public void putBoolean(String key, boolean value) {
        mEditor.putBoolean(key, value);
        mEditor.commit();
    }

    /**
     * 保存token
     *
     * @param token
     */
    public void setToken(String token) {
        putString(TOKEN_KEY, token);
    }

    /**
     * 获取token
     */
    public String getToken() {
        return getString(TOKEN_KEY, "");
    }

    public void setUid(long uid) {
        putLong(UID_KEY, uid);
    }

    /**
     * 获取用户id
     * @return
     */
    public long getUid() {
        return getLong(UID_KEY, 0);
    }


}
